package com.top.service;

import com.top.dao.CategoryFoodDao;
import com.top.dao.DAOFactory;
import com.top.dao.FoodDao;
import com.top.em.MenuEnum;
import com.top.em.TipEnum;
import com.top.entiy.Food;
import com.top.entiy.FoodCategory;
import com.top.utils.FormatUtil;
import com.top.utils.SoutUtil;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

/**
 * @Author:
 * @DateTime: 2023/12/9 12:53
 * @Description: TODO
 */
public class FoodService {

    public void index() {
        System.out.println("-------"+MenuEnum.CATEGORY_FOOD.getTip()+"--------");
        Scanner scanner = new Scanner(System.in);
        System.out.println(TipEnum.COMMON.getTip());
        //新增 修改 删除 菜品列表
        SoutUtil.printButton(MenuEnum.ADD,MenuEnum.EDIT,MenuEnum.DELETE);

        List<Food> all = DAOFactory.getFoodDAOInstance().findAll();
        System.out.println("-------菜品列表--------");
        System.out.println(new Food().getColumn());
        for (Food food : all) {
            System.out.println(food.toString());

        }
        String next = scanner.next();
        try {
            if (next.startsWith("A")) {
                add();
            } else if (next.startsWith("E")) {
                edit();
            } else if (next.startsWith("D")) {
                delete();
            } else if (next.startsWith("Q")) {
                new CommonService().index();
            }else {
                System.err.println("输入错误");
                index();
            }
        } catch (Exception e) {
            System.err.println("输入错误");
            index();
        }
    }

    private void cList() {
        index();
    }

    private void delete() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的菜品名称ID");
        String next = scanner.next();
        String[] split = next.split(",");
        String id = split[0];
        boolean b = DAOFactory.getFoodDAOInstance().doRemove(Integer.valueOf(id));
        if (b) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        index();
    }

    private void edit() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("清输入菜品类别信息进行修改"+new Food().getEditColumn());
        // "ID","菜品名称","价格","描述","热销"
        String next = scanner.next();
        String[] split = next.split(",");
        String id = split[0];
        String name = split[1];
        String price = split[2];
        String info = split[3];
        String hot = split[4];
        Food byName = DAOFactory.getFoodDAOInstance().findByName(name);
        if (byName != null && byName.getId()!=null && !byName.getId().equals(Integer.valueOf(id))) {
            System.out.println("菜品名称已存在");
        } else {
            Food food = new Food();
            food.setId(Integer.valueOf(id));
            food.setName(name);
            food.setPrice(new BigDecimal(price));
            food.setInfo(info);
            food.setHot(Integer.valueOf(hot));
            boolean b = DAOFactory.getFoodDAOInstance().doUpdate(food);
            if (b) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        }
        index();
    }

    private void add() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入菜品类别以创建"+new Food().getAddColumn());
        String next = scanner.next();
        String[] split = next.split(",");
        // ID","菜品名称","价格","描述","热销"
        String name = split[0];
        String price = split[1];
        String info = split[2];
        String hot = split[3];
        //查询菜品列表是否存在
        if (DAOFactory.getFoodDAOInstance().findByName(name) != null) {
            System.out.println("菜品名称已存在");
        } else {
            Food food = new Food();
            food.setName(name);
            food.setPrice(new BigDecimal(price));
            food.setInfo(info);
            food.setHot(Integer.parseInt(hot));
            food.setEnable(1);
            food.setDelete(0);
            food.setCreateTime(new java.util.Date());
            food.setUpdateTime(new java.util.Date());
            boolean b = DAOFactory.getFoodDAOInstance().doCreate(food);
            if (b) {
                System.out.println("创建成功");
            } else {
                System.out.println("创建失败");
            }
        }
        index();
    }

    /**
     * 输出菜单
     * @return
     */
    public void menu() {
        System.out.println("-------热销菜单-------");
        List<Food> hotList = DAOFactory.getFoodDAOInstance().findAllByHot();
        System.out.println(FormatUtil.format(14,"ID","菜品名称","价格","描述","菜品类别","热销"));
        for (Food food : hotList) {
            System.out.println(food.toMenuString());
        }
        System.out.println("-------全部菜单-------");
        List<Food> allList = DAOFactory.getFoodDAOInstance().findAllOrderByCategoryName();
        System.out.println(FormatUtil.format(14,"ID","菜品名称","价格","描述","菜品类别","热销"));
        for (Food food : allList) {
            System.out.println(food.toMenuString());
        }
    }

    /**
     * 根据id查询菜品
     * @param foodId
     * @return
     */
    public Food findById(int foodId) {
        return DAOFactory.getFoodDAOInstance().findById(foodId);
    }
}
